package com.xc.一轮范例;

import java.util.ArrayList;
import java.util.Scanner;

public class Demo1 {

    public static void main(String[] args) {
        test1();
        System.out.println("=====================");
        test2();
    }

    /**
     * @desc    算平均
     * @param heights   身高数组(int[])
     * @return  平均身高(double)
     */
    public static double getAverageHeight(int[] heights) {
        double average = 0;
        for (int height : heights) {
            average += height;
        }
        average /= (double) heights.length;

        return average;
    }

    /**
     * @desc    控制台输入，new数组，循环计算并输出
     */
    public static void test1() {
        int length = 0;
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要输入的数据个数:");
        length = scanner.nextInt();
        int[] heights = new int[length];
        for (int i = 0; i < length; i++) {
            heights[i] = scanner.nextInt();
        }
        double averageHeight = getAverageHeight(heights);
        System.out.println(averageHeight);
    }

    /**
     * @desc    循环遍历数组找到特定的值
     * @param studentsIds   String[]数组
     * @param target    String
     * @return  数组下标(int)
     */
    public static int searchSid(String[] studentsIds, String target) {
        for (int i = 0; i < studentsIds.length; i++) {
            if (target.equals(studentsIds[i])) {
                return i;
            }
        }
        return -1;
    }

    public static void test2() {
        int length = 0;
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要输入的数据个数:");
        length = scanner.nextInt();
        String[] studentsIds = new String[length];
        for (int i = 0; i < length; i++) {
            studentsIds[i] = scanner.nextLine();
        }
        System.out.println("请输入要查找的学号:");
        String target = scanner.nextLine();
        int i = searchSid(studentsIds, target);
        System.out.println(i);
    }









}
